package org.kiwi.util;

import lombok.Data;

/**
 * @email jack.liu.19910921@gmail.com
 * Created by jack on 17/2/19.
 */
@Data
public class BaseResponse<T> {

    public static final int STATUS_OK = 1;
    public static final int STATUS_FAIL = 0;

    public static final String RETURN_CODE_OK = "200";
    public static final String RETURN_CODE_FAIL = "500";

    public static final String RETURN_MESSAGE_OK = "OK";
    public static final String RETURN_MESSAGE_FAIL = "FAIL";

    private int status;
    private String returnCode;
    private String returnMessage;
    private T data;

    private static final BaseResponse SUCCESS = new BaseResponse(STATUS_OK, RETURN_CODE_OK, RETURN_MESSAGE_OK);

    private static final BaseResponse ERROR = new BaseResponse(STATUS_FAIL, RETURN_CODE_FAIL, RETURN_MESSAGE_FAIL);

    public static BaseResponse success() {
        return SUCCESS;
    }

    public static <T> BaseResponse<T> success(T data) {
        return new BaseResponse<>(STATUS_OK, RETURN_CODE_OK, RETURN_MESSAGE_OK, data);
    }

    public static BaseResponse error() {
        return ERROR;
    }

    public static BaseResponse error(String returnCode, String returnMessage) {
        return new BaseResponse(STATUS_FAIL, returnCode, returnMessage);
    }

    public BaseResponse() {
    }

    private BaseResponse(int status, String returnCode, String returnMessage) {
        this.status = status;
        this.returnCode = returnCode;
        this.returnMessage = returnMessage;
    }

    private BaseResponse(int status, String returnCode, String returnMessage, T data) {
        this.status = status;
        this.returnCode = returnCode;
        this.returnMessage = returnMessage;
        this.data = data;
    }

}
